package com.losdun.weixin.security.service;

import com.losdun.weixin.domain.WechatUser;
import com.losdun.weixin.security.vo.WxLoginUser;
import com.losdun.weixin.service.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author Sword.Ye
 * @Date 2025/9/22
 **/
@Service("wxUserDetailsService")
public class WxUserDetailsService implements UserDetailsService {

    @Autowired
    private WechatUserService wechatUserService;
    @Override
    public WxLoginUser loadUserByUsername(String openId) throws UsernameNotFoundException {
        WechatUser user = wechatUserService.getWechatUserByOpenId(openId);
        if(user==null){
            throw new UsernameNotFoundException("用户不存在:"+openId);
        }
        return new WxLoginUser(user.getId(),user.getOpenId(),user);
    }
}
